package com.benjaminwan.read.free.ui.hipda

import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.findNavController
import com.airbnb.mvrx.*
import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.base.adapter.simpleController
import com.benjaminwan.read.free.base.dialog.BaseDialogListFragment
import com.benjaminwan.read.free.common.singleTopNavOptions
import com.benjaminwan.read.free.config.LoginConfig.getLoginConfig
import com.benjaminwan.read.free.model.enums.NetRetState
import com.benjaminwan.read.free.net.RetrofitFactory
import com.benjaminwan.read.free.ui.guide.GuideFragment
import com.benjaminwan.read.free.ui.net.NetViewModel
import com.benjaminwan.read.free.utils.showXToastError
import com.benjaminwan.read.free.utils.showXToastSuccess
import com.benjaminwan.read.free.views.superTextViewItemView
import com.orhanobut.logger.Logger

class LoginFragment : BaseDialogListFragment() {

    private val netVM by activityViewModel(NetViewModel::class)

    private val itemConfigList
        get() = getLoginConfig(netVM)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (RetrofitFactory.isLoggedIn) {
            gotoNext()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        title.text = "登陆Hi-PDA"
        icon.setImageResource(R.drawable.ic_login)
        negative.setOnClickListener { requireDialog().dismiss() }
        positive.setOnClickListener {
            netVM.login()
        }
    }

    private fun showLoading() {
        binding.loginProgressBar.visibility = View.VISIBLE
    }

    private fun hideLoading() {
        binding.loginProgressBar.visibility = View.INVISIBLE
    }

    private fun gotoNext() {
        netVM.getLastVersion()
        findNavController().navigate(
            R.id.guideFragment,
            GuideFragment.arg(true),
            singleTopNavOptions
        )
    }

    override fun invalidate() = withState(netVM) { net ->
        super.invalidate()
        when (net.loginRequest) {
            is Success -> {
                hideLoading()
                val ret = net.loginRequest()
                netVM.resetLoginRequest()
                if (ret?.state == NetRetState.SUCCESS) {
                    Logger.i(ret.toString())
                    showXToastSuccess(ret.msg)
                    gotoNext()
                    this.requireDialog().dismiss()
                } else {
                    showXToastError("登陆失败:${ret?.msg}")
                }
            }
            is Fail -> {
                hideLoading()
                val request = net.loginRequest
                netVM.resetLoginRequest()
                showXToastError("登陆失败:${request.error.message}")
            }
            is Loading -> showLoading()
            else -> {
            }
        }
    }

    override fun epoxyController() = simpleController(netVM) { net ->
        for ((index, item) in itemConfigList.withIndex()) {
            superTextViewItemView {
                id("login_$index")
                enable(true)
                checkEnable(true)
                itemType(item.type)
                leftText(item.leftText)
                rightText(item.rightText)
                isChecked(item.isChecked)
                onClickListener { view ->
                    item.click(view)
                }
                onCheckedChangeListener { buttonView, isChecked ->
                    item.checkedChange(buttonView, isChecked)
                }
            }
        }
        refreshDialog()
    }

}
